// scripts/update_order_organization.js
// 将组织ID为1的订单更新为组织ID为2

const { Sequelize, DataTypes } = require('sequelize');
const config = require('../config/config.default.js')({ name: 'lingxi-system' });

// 创建数据库连接
const sequelize = new Sequelize(
  config.sequelize.database,
  config.sequelize.username,
  config.sequelize.password,
  {
    host: config.sequelize.host,
    port: config.sequelize.port,
    dialect: config.sequelize.dialect,
    timezone: config.sequelize.timezone,
  }
);

// 定义SalesOrder模型
const SalesOrder = sequelize.define('salesOrder', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  organizationId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'organization_id'
  },
  orderNo: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
    field: 'order_no'
  },
  platformOrderId: {
    type: DataTypes.STRING(100),
    allowNull: false,
    unique: true,
    field: 'platform_order_id'
  },
  customerId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'customer_id'
  },
  orderAmount: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    field: 'order_amount'
  },
  orderDate: {
    type: DataTypes.DATE,
    allowNull: false,
    field: 'order_date'
  },
  invoiceStatus: {
    type: DataTypes.ENUM('uninvoiced', 'invoiced', 'sent'),
    defaultValue: 'uninvoiced',
    field: 'invoice_status'
  },
  invoiceDate: {
    type: DataTypes.DATE,
    field: 'invoice_date'
  },
  invoiceNumber: {
    type: DataTypes.STRING(50),
    field: 'invoice_number'
  },
  orderStatus: {
    type: DataTypes.ENUM('pending', 'completed', 'cancelled'),
    defaultValue: 'completed',
    field: 'order_status'
  },
  platformSource: {
    type: DataTypes.STRING(50),
    field: 'platform_source'
  },
  isGift: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
    field: 'is_gift'
  },
  responsibleSalesman: {
    type: DataTypes.STRING(100),
    field: 'responsible_salesman'
  },
  collectionStatus: {
    type: DataTypes.ENUM('pending', 'contacted', 'promised', 'paid', 'difficult'),
    defaultValue: 'pending',
    field: 'collection_status'
  },
  collectionCount: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    field: 'collection_count'
  },
  lastCollectionDate: {
    type: DataTypes.DATE,
    field: 'last_collection_date'
  },
  lastCollectionResult: {
    type: DataTypes.STRING(200),
    field: 'last_collection_result'
  },
  nextFollowupDate: {
    type: DataTypes.DATE,
    field: 'next_followup_date'
  }
}, {
  tableName: 'sales_orders',
  timestamps: false
});

async function updateOrders() {
  try {
    await sequelize.authenticate();
    console.log('数据库连接成功');
    
    // 更新组织ID为1的已开票订单到组织ID为2
    const [updatedRows] = await SalesOrder.update(
      { organizationId: 2 },
      {
        where: {
          organizationId: 1,
          invoiceStatus: 'invoiced'
        }
      }
    );
    
    console.log(`成功更新 ${updatedRows} 个订单的组织ID从1到2`);
    
    // 验证更新结果
    const updatedOrders = await SalesOrder.findAll({
      where: {
        organizationId: 2,
        invoiceStatus: 'invoiced'
      }
    });
    
    console.log(`组织ID为2的已开票订单数: ${updatedOrders.length}`);
    updatedOrders.forEach(order => {
      console.log(`订单ID: ${order.id}, 平台订单号: ${order.platformOrderId}, 开票状态: ${order.invoiceStatus}, 催款状态: ${order.collectionStatus}`);
    });
    
  } catch (error) {
    console.error('更新订单时出错:', error);
  } finally {
    await sequelize.close();
  }
}

// 执行脚本
updateOrders();